package com.uh.microservice.core.util;

import org.apache.commons.collections.map.HashedMap;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by dws on 2018/6/25.
 * EXTJS 主题样式复写，结合less
 *
 * ！！！注意：需要将目标CSS格式化后才能进行匹配。注意文件路径
 */
public class CssOv {

    // 色调列表
    final static String[] COLOR_LIST = {"#eaeff4", "#d3e0ec",
            "#42719a", "#477aa6", "#5795cb", "#5fa2dd", "#518bbd", "#77b0e2",
            "#7fb5e4", "#89bae6", "#99c1e5", "#8bb8f3", "#bfdaf1"};

    public static void main(String[] args) {


        String ENCODING = "UTF-8";
        String cssFilePath =
                "E:\\codegit\\CT3Root\\web-admin\\src\\main\\resources\\static\\js\\lib\\ext\\resources\\";

        String cssFileName = "theme-triton-all_1.format.css";

        String savePath = "E:\\codegit\\CT3Root\\web-admin\\src\\main\\resources\\static\\resources\\less\\";
        String saveName = "style-ov1.less";

        try {

            StringBuilder css = new StringBuilder();

            File cssFile = new File(cssFilePath + cssFileName);
            if (!cssFile.isFile() && !cssFile.exists()) {
                System.out.println("cssFile not exists or not a file");
                return;
            }

            FileInputStream fileInputStream = new FileInputStream(cssFile);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, ENCODING);

            //FileReader fileReader = new FileReader(cssFile);

            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line = null;

            // 用于排除同类定义，暂未启用仅观察
            Map<String, String> filter = new HashedMap();

            List<String> cssBlock = new ArrayList<>();
            boolean append = false;
            int lines = 0;
            while ((line = bufferedReader.readLine()) != null) {
                //System.out.println(line);
                cssBlock.add(line);
                lines++;
                if (line.trim().equals("}") && cssBlock.size() > 0) {
                    if (append) {
                        css.append("\n");
                        css.append("// " + (lines)).append("\n");
                        String cssName = cssBlock.get(1).replace("{", "");
                        StringBuilder filterContent = new StringBuilder();
                        for (String block : cssBlock) {
                            css.append(reDefineColor(block));
                            filterContent.append(block).append("\n");
                        }
                        filter.put(cssName, filterContent.toString());
                    }
                    cssBlock.clear();
                    append = false;
                }

                if (hasColor(line)) {
                    append = true;
                }
            }

            bufferedReader.close();
            inputStreamReader.close();
            fileInputStream.close();

            System.out.println("total lines:" + lines);
            System.out.println(css.toString());
            if (lines <= 1) {
                System.out.println("The CSS file looks problematic!");
                return;
            }
            saveFile(css.toString(), new File(savePath + saveName));

//            System.out.println("\nCSS CLASS LIST:\n");
//            for (String key : filter.keySet()) {
//                System.out.println("css name:" + key);
//            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean hasColor(String line) {

        for (int i = 0; i < COLOR_LIST.length; i++) {
            if (line.indexOf(COLOR_LIST[i]) >= 0) {
                return true;
            }
        }

        return false;
    }

    public static String reDefineColor(String freeStyle) {
        for (int i = 0; i < COLOR_LIST.length; i++) {
            if (freeStyle.indexOf(COLOR_LIST[i]) >= 0) {
                freeStyle = freeStyle.replace(COLOR_LIST[i], "@x" + COLOR_LIST[i].substring(1));
            }
        }

        return freeStyle;
    }

    public static void saveFile(String content, File file) {

        BufferedWriter bufferedWriter;

        try {

            if (file.exists()) {
                file.renameTo(new File(file.getAbsolutePath() + "." + System.currentTimeMillis()));
            } else {
                file.createNewFile();
            }

            FileWriter fileWriter = new FileWriter(file, false);
            bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write("@import url('./style-ov-define.less');\n");
            bufferedWriter.write(content);
            bufferedWriter.close();

            System.out.println("save success!" + file.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


